<script setup>
import {PerfectScrollbar} from 'vue3-perfect-scrollbar'
import {VForm} from 'vuetify/components/VForm'
import {defineEmits, ref} from "vue";
import {useApi} from "@/composables/useApi.js";
import showTost from "@/components/toast/index.js";

const props = defineProps({
  isDrawerOpen: {
    type: Boolean,
    required: true,
  },
  detailData: {
    type: Object,
    required: true
  },
  title: {
    type: String,
    default: '详情'
  },

  gradeList: {
    type: Array,
    required: true
  },
  tabsIndex:{
    type: Number,
    default: 0
  },
  teacherList:{
    type: Array,
  },
  madelList:{
    type: Array,
  }
})

const emit = defineEmits(['update:isDrawerOpen', 'onHandleEdit', 'onHandleDelete'])

const handleDrawerModelValueUpdate = val => {
  emit('update:isDrawerOpen', val)
}

const refVForm = ref()
const resetForm = () => {


  emit('update:isDrawerOpen', false)
}

const closeNavigationDrawer = () => {
  emit('update:isDrawerOpen', false)
  nextTick(() => {
    refVForm.value?.reset()
    refVForm.value?.resetValidation()
  })
}
const form = ref({})
const assignmentData = () => {
  console.log(props.detailData)
  form.value = JSON.parse(JSON.stringify(props.detailData))
  if(form.value.edRadius==1){
    form.value.classId = [-1]
  }
  console.log(form.value)
}

import {storeToRefs} from "pinia"
import {useAppStore} from "../../../store/app"
import {standardAddModify} from "@/api/standard.js";
import {medalAddModify, medalRecordAddModify} from "@/api/medal.js";

const appStore = useAppStore()

const {nationArr, sexArr} = storeToRefs(appStore)
watch(() => props.isDrawerOpen, () => {
  assignmentData()
})
const isEdit = ref(false)
let sexList = ref(['男', '女'])

const onHandleEditData = async () => {
  if (isEdit.value === true) {
    if(form.classId.find(item=>item==-1)){
      form.edRadius = 1
    }else{
      form.edRadius = 2
    }
    const {data} = await useApi('/admin/Api.Honor/add').post(form)
    if (data.value.status == 200) {
      refVForm.value?.reset()
      emit('onHandleEdit')
      isEdit.value = true
      showTost("修改成功", {duration: 3000, type: "success"})
      queryFrom.value.page = 1
      await getList()
    } else {
      console.log(data.value.msg)
      // showTost("新增成功", {duration: 3000, type: "success"})

      showTost(data.value.msg, {duration: 3000, type: "error"})
    }


  } else {
    isEdit.value = true
  }
}
const updatePassword = async () => {
  const {data} = await useApi('/admin/Api.Teacher/resetPwd').post({
    teacher_id: props.detailData.teacher_id,
    password: '000000'
  })
  if (data.value.status == 200) {
    showTost("重置密码成功", {duration: 3000, type: "success"})

    emit('onHandleEdit')
  }
}
const onDelete = () => {
  emit('onHandleDelete', form.value)
}
const required = (value, name) => !!value || `${name}为必填字段`;
const onlyAllowNumbers = (event) => {
  const key = event.key;
  // 检查按键是否为数字或控制键（如 backspace）
  if (!/^\d$/.test(key) && event.key !== 'Backspace' && event.key !== 'Tab') {
    event.preventDefault();
  }
}
const filterInput =(key) => {
  // 仅保留数字
  form.value[key] = form.value[key].replace(/\D/g, '');
}
let totalScore = computed(()=>{
  return Number(form.value.languageScore)    +  Number(form.value.mathsScore) + Number(form.value.englishScore)
})
watch(()=>form.student_id,val=>{
  console.log(val)
  if(val==null){
    form.classTitle = ''
    form.gradeTitle = ''
    return
  }

})
const projectType = ref([{title:'标化成绩',value:1},{title:'素质分数',value:2}])

const projectList = ref([{title:'英语',value:1},{title:'数学',value:2},{title:'语文',value:3},{title:'美术',value:4},{title:'总分',value:5}],

)
const qualityList = [{title:'智',value:1},{title:'体',value:1},
  {title:'美',value:1},{title:'劳',value:1}]

const getTypeList = ref([{title:'分值',value:1},{ title:'排名',value:2},{ title:'百分比排名',value:3}])


const judgment = ref([{title:'>=',value:1},{ title:'=',value:2},{ title:'>',value:3},{ title:'<',value:4},{ title:'<=',value:5}])

</script>

<template>
  <VNavigationDrawer
    :model-value="props.isDrawerOpen"
    temporary
    location="end"
    width="370"
    @update:model-value="handleDrawerModelValueUpdate"
  >
    <!-- 👉 Header -->
    <AppDrawerHeaderSection
      :title="title"
      @cancel="closeNavigationDrawer"
    />
    <VDivider/>

    <VCard flat>
      <PerfectScrollbar
        :options="{ wheelPropagation: false }"
        class="h-100"
      >
        <VCardText style="block-size: calc(100dvh - 5rem);display: flex;flex-direction: column;">
          <VForm
            :disabled="!isEdit"
            ref="refVForm"
            @submit.prevent=""
          >
          <VRow>
            <VCol cols="6">
              <VTextField
                v-model="form.title"
                label="荣誉名称"
                :rules="[(val)=>required(val,'荣誉名称')]"
              >
                <template v-slot:label>
                  <span>荣誉名称</span>
                </template>
              </VTextField>
            </VCol>
            <VCol cols="6">
              <VTextField
                v-model="form.studentName"
                label="学生姓名"
                :rules="[(val)=>required(val,'学生姓名')]"
              >
                <template v-slot:label>
                  <span>学生姓名</span>
                </template>
              </VTextField>
            </VCol>
            <VCol cols="6">
              <VTextField
                v-model="form.gradeTitle"
                label="年级"
                :rules="[(val)=>required(val,'年级')]"
              >
                <template v-slot:label>
                  <span>年级</span>
                </template>
              </VTextField>
            </VCol>
            <VCol cols="6">
              <VTextField
                v-model="form.classTitle"
                label="班级"
                :rules="[(val)=>required(val,'班级')]"
              >
                <template v-slot:label>
                  <span>班级</span>
                </template>
              </VTextField>
            </VCol>
            <VCol cols="12">
              <VTextField
                v-model="form.create_time"
                label="获得时间"
              >
                <template v-slot:label>
                  <span>获得时间</span>
                </template>
              </VTextField>
            </VCol>

            <VCol cols="12">
              <div class="mb-1">荣誉图标</div>
              <upload-ohter-img
                :disabled="!isEdit"
                style="width:100%;"
                :limit="1"
                :value="form.icon"
                @input="(val) => {form.icon = val}"
              />
            </VCol>
            </VRow>
          </VForm>
          <div class="d-flex justify-end" style="height:60px;margin-top: auto">
<!--            <VBtn-->
<!--              color="primary"-->
<!--              class="me-4"-->
<!--              @click="onHandleEditData"-->
<!--            >-->
<!--              {{ isEdit == true ? '保存' : '编辑' }}-->
<!--            </VBtn>-->
<!--            <VBtn-->
<!--              color="error"-->
<!--              variant="outlined"-->
<!--              @click="onDelete"-->
<!--            >-->
<!--              删除-->
<!--            </VBtn>-->
          </div>
        </VCardText>
      </PerfectScrollbar>
    </VCard>
  </VNavigationDrawer>
</template>

<style lang="scss">
.v-navigation-drawer__content {
  overflow-y: hidden !important;
}
</style>
